package tw.asap.plus.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import tw.asap.plus.R;
import tw.asap.plus.common.ConstValue;
import tw.asap.plus.entity.request.CommonRequestParams;
import tw.asap.plus.entity.response.Address;
import tw.asap.plus.entity.response.ResponseDataWrapper;
import tw.asap.plus.network.config.HRetrofitCreator;
import tw.asap.plus.network.service.UserService;
import tw.asap.plus.ui.AsapBaseActivity;
import tw.asap.plus.ui.adapter.AddressAdapter;
import tw.asap.plus.ui.custom.AppBar;
import tw.asap.plus.utils.Toaster;

/**
 * Created by Yax.
 * Date: 15/11/14
 * Time: 下午3:32
 * Description:
 */
public class AddressListActivity extends AsapBaseActivity implements View.OnClickListener {


    @InjectView(R.id.appbar)
    AppBar appbar;
    @InjectView(R.id.address_list)
    ListView addressList;

    public static final int ADDRESS_NEW = 0x00;
    public static final int ADDRESS_EDIT = 0x01;
    public static final int ADDRESS_CHOOSE = 0x02;

    private String currentSelectId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_address_list);
        ButterKnife.inject(this);
        appbar.setRightTitle(getString(R.string.new_address), this);
        if (getIntent() != null) {
            currentSelectId = getIntent().getStringExtra("ADDRESS_ID");
            if (getIntent().getBooleanExtra("FROM_MINE", false)) {
                appbar.setTitle(getString(R.string.my_address));
            }

        }
        refreshAddressList();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_navigation_header_right:
                Intent intent = new Intent(AddressListActivity.this, AddressEditActivity.class);
                startActivityForResult(intent, AddressListActivity.ADDRESS_NEW);
                break;
        }
    }

    private void refreshAddressList() {
        HRetrofitCreator creator = HRetrofitCreator.getInstance();
        Observable.create(new Observable.OnSubscribe<ResponseDataWrapper<List<Address>>>() {
            @Override
            public void call(Subscriber<? super ResponseDataWrapper<List<Address>>> subscriber) {
                UserService service = creator.getService(UserService.class);
                subscriber.onNext(service.getAddress(new CommonRequestParams()));
                subscriber.onCompleted();
            }
        }).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io())
                .subscribe(new Subscriber<ResponseDataWrapper<List<Address>>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(ResponseDataWrapper<List<Address>> addressList) {
                        if (addressList.respcd.equals(ConstValue.RESPCD)) {
                            initAddressList(addressList.data);
                        } else {
                            Toaster.s(AddressListActivity.this, addressList.respmsg);
                        }
                    }
                });
    }

    private void initAddressList(List<Address> data) {
        AddressAdapter adapter = new AddressAdapter(this, data, currentSelectId);
        addressList.setAdapter(adapter);
        addressList.setOnItemClickListener(adapter);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode) {
            case ADDRESS_EDIT:
            case ADDRESS_NEW:
                if (resultCode == RESULT_OK)
                    refreshAddressList();
                break;
        }

    }
}
